/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.datamodel;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author bob50
 * @param <T>
 */
public  class SimpleTableModel<T> extends AbstractTableModel
{
   protected List<String> cols;   //列的名称
   protected List<T> rows;        //数据集合
   protected List<String>  colNames;   //表字段名称
   private List<Method> getMethods;
   
           
    public SimpleTableModel(List<String> cols, List<T> rows,List<String> colNames) {
        this.cols = cols;
        this.rows = rows;
        this.colNames = colNames;
        if(rows.get(0)!=null&&rows.size()>0)
        this.getMethods=ClassRefect.getRequireGetMethod(rows.get(0),colNames);
    }

    public List<String> getCols() {
        return cols;
    }

    public void setCols(List<String> cols) {
        this.cols = cols;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
    
    

    @Override
    public int getRowCount() {
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return  cols.size();
    }

    @Override
    public String getColumnName(int column) {
        return cols.get(column);
    }
    

    @Override
    public  Object getValueAt(int rowIndex, int columnIndex) {
       try {
           //List<Method> getMethods=ClassRefect.getRequireGetMethod(rows.get(rowIndex),colNames);
           if(getMethods.size()>0)
           return getMethods.get(columnIndex).invoke(rows.get(rowIndex), null);
           else return null;
       } catch (IllegalAccessException ex) {
           Logger.getLogger(SimpleTableModel.class.getName()).log(Level.SEVERE, null, ex);
       } catch (IllegalArgumentException ex) {
           Logger.getLogger(SimpleTableModel.class.getName()).log(Level.SEVERE, null, ex);
       } catch (InvocationTargetException ex) {
           Logger.getLogger(SimpleTableModel.class.getName()).log(Level.SEVERE, null, ex);
       }
       return "";
    }
}
